//
//  KomplectCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

final class KomplektCell: UITableViewCell {
    
    private lazy var titlelLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .systemFont(ofSize: 17, weight: .bold)
        return lbl
    }()
    
    private lazy var itemStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .leading
        stack.distribution = .fillEqually
        stack.spacing = 6
        return stack
    }()
    
    private lazy var separatorView = CellSeparator()
    
    
    // MARK: - Props
    
    public var data: KomplektViewModel.KomplektSection! {
        didSet {
            self.titlelLbl.text = "\(data.title) (\(data.items.count))"
            if data.items.count != 0 {
                
                if (data.isExpanded) {
                    self.addStack()
                    self.addItems(data.items)
                } else {
                    self.removeStack()
                }
                
            } else {
                self.removeStack()
            }
        }
    }
    
    private var separatorTop: Constraint?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addItems (_ items: [String]) {
        
        _ = itemStack.arrangedSubviews.map {
            itemStack.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        for item in items {
            let itemLbl = self.createItem(text: item)
            itemLbl.snp.makeConstraints { (make) in
                make.height.equalTo(24)
            }
            itemStack.addArrangedSubview(itemLbl)
        }
    }
    
    private func createItem (text: String) -> UILabel {
        let lbl = UILabel()
        lbl.textColor = .greyColor
        lbl.font = .systemFont(ofSize: 15)
        lbl.text = text
        lbl.numberOfLines = 0
        lbl.textAlignment = .left
        lbl.sizeToFit()
        return lbl
    }
    
    private func addViews () {
        contentView.addSubview(titlelLbl)
        contentView.addSubview(separatorView)
        
        titlelLbl.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(22)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(20)
        }
        separatorView.snp.makeConstraints { (make) in
            make.bottom.leading.trailing.equalToSuperview()
            separatorTop = make.top.equalTo(titlelLbl.snp.bottom).offset(22).constraint
        }
    }
    
    private func addStack () {
        contentView.addSubview(itemStack)
        separatorTop?.deactivate()
        
        itemStack.snp.makeConstraints { (make) in
            make.top.equalTo(titlelLbl.snp.bottom).offset(22)
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(separatorView.snp.top).offset(-22)
        }
        separatorView.snp.makeConstraints { (make) in
            separatorTop = make.top.equalTo(itemStack.snp.bottom).offset(16).constraint
        }
    }
    
    private func removeStack () {
        
        _ = itemStack.arrangedSubviews.map {
            itemStack.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        
        separatorTop?.deactivate()
        itemStack.removeFromSuperview()
        
        separatorView.snp.makeConstraints { (make) in
            separatorTop = make.top.equalTo(titlelLbl.snp.bottom).offset(22).constraint
        }
    }
}
